home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / symbol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  3.7 KB  |  172 lines  |  [TEXT/ttxt]

  1. /*
  2.  
  3.    symbol.c
  4.  
  5.    This software is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This software is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with this software; if not, write to the Free
  17.    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.    Original copyright notice follows:
  20.  
  21.    Copyright, 1993, Brent Benson.  All Rights Reserved.
  22.    0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.
  23.  
  24.    Permission to use, copy, and modify this software and its
  25.    documentation is hereby granted only under the following terms and
  26.    conditions.  Both the above copyright notice and this permission
  27.    notice must appear in all copies of the software, derivative works
  28.    or modified version, and both notices must appear in supporting
  29.    documentation.  Users of this software agree to the terms and
  30.    conditions set forth in this notice.
  31.  
  32.  */
  33.  
  34. #include <string.h>
  35. #include <ctype.h>
  36.  
  37. #include "symbol.h"
  38.  
  39. #include "alloc.h"
  40. #include "bytestring.h"
  41.  
  42. /* local function prototypes 
  43.  */
  44. static Object intern_symbol (char *name);
  45.  
  46. #ifdef NO_STRCASECMP
  47. int strcasecmp (unsigned char *s1, unsigned char *s2);
  48.  
  49. #endif
  50. static char *tolowerstr (char *str);
  51.  
  52. /* local data 
  53.  */
  54.  
  55. /* If SYMTAB_SIZE is not a power of 2, see change required below. */
  56. #define SYMTAB_SIZE 1024
  57. struct symtab *symbol_table[SYMTAB_SIZE];
  58. unsigned char chartable[1 << sizeof (char) * 8];
  59.  
  60. /* function definitions 
  61.  */
  62. void
  63. init_symbol_prims (void)
  64. {
  65.     int c;
  66.  
  67.     for (c = 0; c < (1 << sizeof (char) * 8); c++) {
  68.     chartable[c] = tolower (c);
  69.     }
  70. }
  71.  
  72. Object
  73. make_symbol (char *name)
  74. {
  75.     Object obj;
  76.  
  77.     obj = intern_symbol (name);
  78.     return (obj);
  79. }
  80.  
  81. Object
  82. make_keyword (char *name)
  83. {
  84.     Object obj;
  85.  
  86.     obj = intern_symbol (name);
  87.     SYMBOLTYPE (obj) = Keyword;
  88.     return (obj);
  89. }
  90.  
  91.  
  92. Object
  93. make_setter_symbol (Object sym)
  94. {
  95.     char *name;
  96.  
  97.     name = allocate_string (sizeof (char) * (1 + strlen (SYMBOLNAME (sym)) +
  98.                          strlen ("-setter")));
  99.  
  100.     strcpy (name, SYMBOLNAME (sym));
  101.     strcat (name, "-setter");
  102.     return (make_symbol (name));
  103. }
  104.  
  105. #ifdef NO_STRCASECMP
  106. int
  107. strcasecmp (unsigned char *s1, unsigned char *s2)
  108. {
  109.     while ((chartable[*s1] == chartable[*s2++])) {
  110.     if (!chartable[*s1++])
  111.         return 0;
  112.     }
  113.     return (chartable[*s1] - chartable[*--s2]);
  114. }
  115. #endif
  116.  
  117. static char *
  118. tolowerstr (char *str)
  119. {
  120.     char *s = str;
  121.  
  122.     while (*s) {
  123.     *s = tolower (*s);
  124.     ++s;
  125.     }
  126.     return str;
  127. }
  128.  
  129. static Object
  130. intern_symbol (char *name)
  131. {
  132.     int i;
  133.     unsigned h;
  134.     struct symtab *entry;
  135.     Object sym;
  136.  
  137.     h = i = 0;
  138.     while (name[i]) {
  139.     h += tolower (name[i++]);
  140.     }
  141. /*
  142.    h = h % SYMTAB_SIZE;
  143.  */
  144.  
  145.     /* Works only if SYMTAB_SIZE is a power of 2 */
  146.     h &= (SYMTAB_SIZE - 1);
  147.  
  148.     entry = symbol_table[h];
  149.     while (entry) {
  150. #ifdef NO_STRCASECMP
  151.     if (strcasecmp ((unsigned char *) name,
  152.             (unsigned char *) SYMBOLNAME (entry->sym)) == 0) {
  153. #else
  154.     if (strcasecmp (name, SYMBOLNAME (entry->sym)) == 0) {
  155. #endif
  156.         return (entry->sym);
  157.     }
  158.     entry = entry->next;
  159.     }
  160.  
  161.     /* not found, create new entry for it. */
  162.     sym = allocate_object (sizeof (struct symbol));
  163.  
  164.     SYMBOLTYPE (sym) = Symbol;
  165.     SYMBOLNAME (sym) = checking_strdup (name);
  166.     entry = (struct symtab *) allocate_symtab ();
  167.     entry->sym = sym;
  168.     entry->next = symbol_table[h];
  169.     symbol_table[h] = entry;
  170.     return (sym);
  171. }
  172.